import java.util.Arrays;
import java.util.HashMap;

public abstract class Player {

	private Card[] hand;
	private int chips;
	private String name;
	private String icon = "Default.jpg";
	
	public Player()
	{
		hand = new Card[5];
		chips = Game.DEFAULT_CHIPS;
		name = "";
		icon = "Default.jpg";
	}
	
	public void draw() {
		
	}
	
	public Card[] getHand()
	{
		return hand;
	}
	
	public void removeChips(int x){
		chips -= x;
	}
	
	public void gainChips(int x){
		chips += x;
	}
	
	public int getChips(){
		return chips;
	}

	public void setImage(String x) {
		icon = x;
	}
	
	public String serialize()
	{
		String returnValue = "<player>";
		returnValue += "<name>"+name+"</name>";
		returnValue += "<icon>"+icon+"</icon>";
		returnValue += "<chips>"+chips+"</chips>";
		returnValue += "<hand>";
		for(Card x : hand)
			returnValue += x.toString();
		returnValue += "</hand>";
		returnValue += "</player>";
		
		return returnValue;
	}
	public void deserialize(String playerValues)
	{
		
	}
	
	private boolean isFlush (Card[] hand)
	{
		CardSuit suit = hand[0].getSuit();
		for(Card x : hand)
			if(x.getSuit() != suit) return false;
				
		return true;
	}
	
	private HashMap hashMapOfCardValues (Card[] hand)
	{
		HashMap values = new HashMap(CardValue.values().length);
		for(CardValue x : CardValue.values())
			values.put(x.value(),(int)0);
		for(Card x : hand)
			values.put(x.getValue(),Integer.parseInt(values.get(x.getValue()).toString())+1);
		return values;
	}
	
	private boolean isStraight (Card[] hand)
	{
		HashMap values = hashMapOfCardValues(hand);
		
		int sequential = 0;
		for(CardValue x : CardValue.values())
		{
			if(Integer.parseInt(values.get(x.value()).toString()) > 0) 
				sequential++;
			else sequential = 0;
		}
		
		return (sequential >= 5);
	}
	private boolean isMultiple (Card[] hand, int multiple)
	{
		HashMap values = hashMapOfCardValues(hand);
		
		for(CardValue x : CardValue.values())
			if(Integer.parseInt(values.get(x.value()).toString()) == multiple) 
				return true;
				
		return false;
	}
	private boolean isTwoPair (Card[] hand)
	{
		HashMap values = hashMapOfCardValues(hand);
		
		int pairs = 0;
		for(CardValue x : CardValue.values())
			if(Integer.parseInt(values.get(x.value()).toString())>=2) pairs++;
				
		return (pairs>=2);
	}
	
	public double rankHand()
	{
		if(isFlush(hand) && isStraight(hand))
			return 9;
		else if (isMultiple(hand, 4))
			return 8;
		else if (isMultiple(hand, 3) && isMultiple(hand, 2))
			return 7;
		else if (isFlush(hand))
			return 6;
		else if (isStraight(hand))
			return 5;
		else if (isMultiple(hand, 3))
			return 4;
		else if (isTwoPair(hand))
			return 3;
		else if (isMultiple(hand, 2))
			return 2;
		else 
			return 1;		
	}

}